package com.weayer.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.weayer.constant.CommonStatusEnum;
import com.weayer.constant.DriverConstants;
import com.weayer.dto.*;
import com.weayer.mapper.CarMapper;
import com.weayer.mapper.DriverCarBindingRelationshipMapper;
import com.weayer.mapper.DriverUserMapper;
import com.weayer.mapper.DriverUserWorkStatusMapper;
import com.weayer.response.OrderDriverResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description: Service服务 - 司机 - 司机管理Service
 * @author: Weayer
 **/
@Service
public class DriverUserService {
    @Autowired
    private CarMapper carMapper;
    @Autowired
    private DriverUserMapper driverUserMapper;
    @Autowired
    private DriverUserWorkStatusMapper driverUserWorkStatusMapper;
    @Autowired
    private DriverCarBindingRelationshipMapper driverCarBindingRelationshipMapper;

    /**
     * @Description: 添加司机工作状态
     * @Param: [driverUser]
     * @return: com.weayer.dto.ResponseResult
     **/
    public ResponseResult addDriverUser(DriverUser driverUser){
        LocalDateTime now = LocalDateTime.now();
        driverUser.setGmtCreate(now);
        driverUser.setGmtModified(now);
        driverUserMapper.insert(driverUser);
//        driverUserMapper.selectById(1584359006294835202);

        DriverUserWorkStatus driverUserWorkStatus = new DriverUserWorkStatus();
        driverUserWorkStatus.setDriverId(driverUser.getId());
        driverUserWorkStatus.setWorkStatus(DriverConstants.DRIVER_WORK_STATUS_STOP);
        driverUserWorkStatus.setGmtCreate(now);
        driverUserWorkStatus.setGmtModified(now);
        driverUserWorkStatusMapper.insert(driverUserWorkStatus);
        return ResponseResult.success("");
    }
    /**
     * @Description: 修改司机工作状态
     * @Param: [driverUser]
     * @return: com.weayer.dto.ResponseResult
     **/
    public ResponseResult updateDriverUser(DriverUser driverUser){
        LocalDateTime now = LocalDateTime.now();
        driverUser.setGmtModified(now);
        driverUserMapper.updateById(driverUser);
        return ResponseResult.success("");
    }
    /**
     * @Description: 根据id查询车辆信息
     * @Param: [driverPhone]
     * @return: com.weayer.dto.ResponseResult<com.weayer.dto.DriverUser>
     **/
    public ResponseResult<DriverUser> getDriverUserByPhone(String driverPhone){
        Map<String, Object> map = new HashMap<>();
        map.put("driver_phone", driverPhone);
        map.put("state", DriverConstants.DRIVER_USER_VALID);
        List<DriverUser> driverUsers = driverUserMapper.selectByMap(map);
        if(driverUsers.isEmpty()){
            return ResponseResult.fail(CommonStatusEnum.DRIVER_NOT_EXISTS.getCode(), CommonStatusEnum.DRIVER_NOT_EXISTS.getValue());
        }else{
            return ResponseResult.success(driverUsers.get(0));
        }
    }
    /**
     * @Description: 根据carId查询绑定的司机
     * @Param: [carId]
     * @return: com.weayer.dto.ResponseResult<com.weayer.response.OrderDriverResponse>
     **/
    public ResponseResult<OrderDriverResponse> getAvailableDriver(Long carId){
        // 1. 查询 driver_car_binding_relationship 表，绑定该车辆的司机
        QueryWrapper<DriverCarBindingRelationship> relationQueryWrapper = new QueryWrapper<>();
        relationQueryWrapper.eq("car_id", carId);
        relationQueryWrapper.eq("bind_state", DriverConstants.DRIVER_CAR_BIND);
        DriverCarBindingRelationship relationship = driverCarBindingRelationshipMapper.selectOne(relationQueryWrapper);
        Long driverId = relationship.getDriverId();
        // 2. 查询 driver_user_work_status 表，要求该司机工作状态正常
        QueryWrapper<DriverUserWorkStatus> statusQueryWrapper = new QueryWrapper<>();
        statusQueryWrapper.eq("driver_id", driverId);
        statusQueryWrapper.eq("work_status", DriverConstants.DRIVER_WORK_STATUS_START);
        DriverUserWorkStatus driverUserWorkStatus = driverUserWorkStatusMapper.selectOne(statusQueryWrapper);
        if(null == driverUserWorkStatus){
            return ResponseResult.fail(CommonStatusEnum.AVAILABLE_DRIVER_EMPTY.getCode(), CommonStatusEnum.AVAILABLE_DRIVER_EMPTY.getValue());
        }
        // 3. 查询 driver_user 表，获取该司机信息
        QueryWrapper<DriverUser> driverQueryWrapper = new QueryWrapper<>();
        driverQueryWrapper.eq("id", driverId);
        DriverUser driverUser = driverUserMapper.selectOne(driverQueryWrapper);
        // 4. 查询 car 表，获取车辆信息
        QueryWrapper<Car> carQueryWrapper = new QueryWrapper<>();
        carQueryWrapper.eq("id", carId);
        Car car = carMapper.selectOne(carQueryWrapper);

        OrderDriverResponse response = new OrderDriverResponse();
        response.setCarId(carId);
        response.setDriverId(driverId);
        response.setDriverPhone(driverUser.getDriverPhone());
        response.setLicenseId(driverUser.getLicenseId());
        response.setVehicleNo(car.getVehicleNo());
        response.setVehicleType(car.getVehicleType());
        return ResponseResult.success(response);
    }
}
